<?php
namespace app\admin\controller;

use app\admin\exception\AdminException;
use think\Request;


class Custom extends Admin
{

    public function initialize() {
        parent::initialize();
        $this->service = model('custom/custom', 'service');
    }
    /**
     * @power 客户管理|客户列表
     * @rank 1
     */
    public function index(Request $request)
    {
        $maps = [];
        $maps['type'] = $request->get('type');
        $maps['keywords'] = $request->get('keywords');
        $maps['parent_current_id'] = '';
        $list = $this->service->lists($maps,'create_time DESC',$this->page,$this->limit,true);
        return $this->render('index', [
            'list' => $list,
        ]);
    }

    /**
     * @power 客户管理|客户列表@创建客户
     */
    public function create()
    {

        return $this->render('edit');
    }

    /**
     * @power 客户管理|客户列表@编辑客户
     */
    public function edit($current_id)
    {
        $custom = $this->service->detail(['current_id'=>$current_id]);
        if (!$custom) {
            $this->error('客户不存在');
        }

        return $this->render('edit', [
            'info' => $custom,
        ]);
    }

    /**
     * @power 客户管理|客户列表@添加客户
     */
    public function save(Request $request)
    {
        $result = $this->validate($request->post(), 'app\custom\validate\Custom.add');

        if (true !== $result) {
            return json()->data(['code' => 1, 'message' => $result]);
        }

        $result = $this->service->create($request->post());

        if (!$result) {
            throw new AdminException($this->service->getError());
        }

        return json(['code' => 0, 'toUrl' => url('/admin/custom'), 'message' => '创建成功']);
    }

    /**
     * @power 客户管理|客户列表@编辑客户
     */
    public function update(Request $request, $current_id)
    {
        $custom = $this->service->detail(['current_id'=>$current_id]);
        if (!$custom) {
            $this->error('客户不存在');
        }

        $result = $this->validate($request->post(), 'app\custom\validate\Custom.edit');

        if (true !== $result) {
            return json()->data(['code' => 1, 'message' => $result]);
        }

        $result = $this->service->save($request->post(),$current_id);

        if (!$result) {
            throw new AdminException($this->service->getError());
        }

        return json(['code' => 0, 'toUrl' => url('/admin/custom'), 'message' => '编辑成功']);

    }

    /**
     * @power 客户管理|客户列表@客户充值
     */
    public function recharge(Request $request, $current_id)
    {
        if ($request->isPost()){
            $result = $this->validate($request->post(), 'app\custom\validate\Custom.recharge');
            if (true !== $result) {
                return json()->data(['code' => 1, 'message' => $result]);
            }
            $result = $this->service->recharge($current_id,$request->post('money'),$request->post('remark'));
            if (!$result) {
                throw new AdminException($this->service->getError());
            }
            return json(['code' => 0, 'toUrl' => url('/admin/custom'), 'message' => '充值成功']);
        }
        $info = $this->service->detail(['current_id'=>$current_id]);
        return $this->render('recharge', [
            'info' => $info,
        ]);

    }

    /**
     * @power 客户管理|客户列表@客户扣费
     */
    public function deduction(Request $request, $current_id)
    {
        if ($request->isPost()){
            $result = $this->validate($request->post(), 'app\custom\validate\Custom.deduction');
            if (true !== $result) {
                return json()->data(['code' => 1, 'message' => $result]);
            }
            $result = $this->service->deduction($current_id,$request->post('money'),$request->post('remark'));
            if (!$result) {
                throw new AdminException($this->service->getError());
            }
            return json(['code' => 0, 'toUrl' => url('/admin/custom'), 'message' => '扣费成功']);
        }
        $info = $this->service->detail(['current_id'=>$current_id]);
        return $this->render('deduction', [
            'info' => $info,
        ]);

    }

    /**
     * @power 客户管理|客户列表@删除客户
     */
    public function delete($current_id)
    {
        $result = $this->service->destroy($current_id);
        if (!$result) {
            throw new AdminException($this->service->getError());
        }

        return json(['code' => 0, 'message' => '删除成功']);
    }

    /**
     * @power 客户管理|客户列表@禁用客户
     */
    public function forbidden($current_id)
    {
        $result = $this->service->save(['status'=>-1],$current_id);
        if (!$result) {
            throw new AdminException($this->service->getError());
        }

        return json(['code' => 0, 'message' => '禁用成功']);
    }

    /**
     * @power 客户管理|客户列表@解禁客户
     * @method POST
     */
    public function unforbidden($current_id)
    {
        $result = $this->service->save(['status'=>1],$current_id);
        if (!$result) {
            throw new AdminException($this->service->getError());
        }

        return json(['code' => 0, 'message' => '解禁成功']);
    }
}